package com.ljh.extensions.rabbit.controller;

import com.ljh.extensions.rabbit.constants.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author: Themberfue
 * @date: 2025/4/30 15:54
 * @description:
 */
@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    RabbitTemplate confirmRabbitTemplate;

    @Resource(name = "transRabbitTemplate")
    RabbitTemplate transRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE, "ack", "消费者消息确认喵~");
        return "发送成功";
    }

    @RequestMapping("/persist")
    public String persist() {
        Message message = new Message("消费者消息确认喵~".getBytes(StandardCharsets.UTF_8), new MessageProperties());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.PERSIST_EXCHANGE, "persist", message);
        return "发送成功";
    }

    @RequestMapping("/confirm")
    public String confirm() {
        // ! 直接使用 setConfirmCallback 会影响其他接口的调用
        // ! 且只能设置一个确认回调，多次发起请求会报错
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("执行了confirm方法");
//                if (ack) {
//                    System.out.printf("接收到消息，消息ID：%s\n",
//                            correlationData == null ? null : correlationData.getId());
//                } else {
//                    System.out.printf("未接收到消息，消息ID：%s\n；原因：%s",
//                            correlationData == null ? null : correlationData.getId(), cause);
//                }
//            }
//        });
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE + "1", "confirm", "confirm test...", correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/returns")
    public String returns() {
        CorrelationData correlationData = new CorrelationData("5");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE, "confirm1", "returns test...", correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, "retry", "retry test...");
        return "消息发送成功";
    }

    @RequestMapping("/ttl")
    public String ttl() {
//        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                message.getMessageProperties().setExpiration("10000"); // ? 设置消息的 TTL 值
//                return message;
//            }
//        };

        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test...", message -> {
            message.getMessageProperties().setExpiration("10000"); // ? 设置消息的 TTL 值
            return message;
        });

        return "消息发送成功";
    }

    @RequestMapping("/dl")
    public String dl() {
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "normal test...");
        return "消息发送成功：" + new Date();
    }

    @RequestMapping("/delay")
    public String delay() {
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test...", message -> {
            message.getMessageProperties()
                    .setDelayLong(20_000L); // ? 设置消息的延迟发送时间
            return message;
        });

        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test...", message -> {
            message.getMessageProperties()
                    .setDelayLong(10_000L); // ? 设置消息的延迟发送时间
            return message;
        });
        return "消息发送成功：" + new Date();
    }

    @Transactional
    @RequestMapping("/trans")
    public String trans() {
        String msg = "trans test...";
        System.out.println("发送第一条消息：" + msg + 1);
        transRabbitTemplate.convertAndSend("", Constants.TRANS_QUEUE, msg);

        int a = 3 / 0;

        System.out.println("发送第二条消息：" + msg + 2);
        transRabbitTemplate.convertAndSend("", Constants.TRANS_QUEUE, msg);

        return "消息发送成功：";
    }

    @RequestMapping("/qos")
    public String qos() {
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE, "qos", "qos test...");
        }
        return "消息发送成功：" + new Date();
    }
}
